﻿/****---------------------------------------------------------------------------------------------------->>>>
*	PopUpController for Flash 8 [v1.0]
*	Coded to work with ActionScript 2.0
*	--------------------------------------------------
*	Created by
*		Lance Den (vistaden@msn.com , yanjun@vika.cn)
*		SIAN Digital Technology co.,ltd. (www.vika.cn)
*
*	Based on code by
*		(null)
*
*	Version History:
*		2007-05-22 - Estabished by Lance Den.
*
*	Authors notes:
*		Class Initialization:
* 			- this is a core class for the project, it's very important.
*
*			Example:
*				null
*
*			Note:
*				null
*
*			Example:
*				
*
*		- Lance Den 2007-05-22
<<<<----------------------------------------------------------------------------------------------------****/

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import mx.events.EventDispatcher;
import mx.utils.Delegate;

class mx.utils.BitmapDataController
{

   // public var addListener:Function
    //public var broadcastMessage:Function

    private var id:Number;
    private var $pixeldata:Object;
	
	// init EventDispatcher
	public var addEventListener:Function;
	public var removeEventListener:Function;
	private var dispatchEvent:Function;
	private var dispatchQueue:Function;
	private static var _mixin1 = EventDispatcher.initialize(BitmapDataController.prototype);

    function BitmapDataController(){
		//AsBroadcaster.initialize( this );
		this.$pixeldata = new Object;
		this.$pixeldata.width  = 0;
		this.$pixeldata.height = 0;
		this.$pixeldata.cols   = 0;
		this.$pixeldata.rows   = 0;
		//-->> array
		$pixeldata.data   = new Array();
    }
	
    public function printMovieClip(mc, x:Number, y:Number, w:Number, h:Number,sx:Number,sy:Number):BitmapData
	{
		if(x == undefined)  x  = 0;
		if(y == undefined)  y  = 0;
		if(w == undefined)  w  = mc._width;
		if(h == undefined)  h  = mc._height;
		if(sx == undefined) sx = 1;
		if(sy == undefined) sy = 1;
		
		var t_bmp:BitmapData = new BitmapData(w, h, true,0);
		
		var t_matrix = new Matrix();
		t_matrix.translate(-x, -y);
		t_matrix.scale(sx, sy);
		
		t_bmp.draw(mc, t_matrix, new ColorTransform(), 1, new Rectangle(0, 0, w, h));
		// 20070524 t_bmp.draw(mc, t_matrix);
		
		return t_bmp;
    }
	
	/*
	*/
	public function generateBMPData(mc:MovieClip, x:Number, y:Number, w:Number, h:Number,sx:Number,sy:Number)
	{
		if(x  == undefined) x  = 0;
        if(y  == undefined) y  = 0;
        if(w  == undefined) w  = mc._width;
        if(h  == undefined) h  = mc._height;
		if(sx == undefined) sx = 1;
        if(sy == undefined) sy = 1;
		
		this.$pixeldata.width  = w;
        this.$pixeldata.height = h;
        this.$pixeldata.cols   = 0;
        this.$pixeldata.rows   = 0;
		this.$pixeldata.data   = new Array();
		
		var t_bmp:BitmapData = this.printMovieClip(mc, x, y, w, h,sx,sy);
		id = setInterval(this,"copysource", 5, mc, t_bmp);
	}
	
    private function copysource(mc, bmp)
	{
        var pixel:Number
        var str_pixel:String
        this.$pixeldata.data["px" + String(this.$pixeldata.rows)] = new Array();
        for(var a = 0; a < bmp.width; a++){
            pixel     = bmp.getPixel(a, this.$pixeldata.rows)
            str_pixel = pixel.toString(16)
            //-->> don't send blank pixel
			if(pixel == 0xFFFFFF) str_pixel = "";
            this.$pixeldata.data["px" + String(this.$pixeldata.rows)].push(str_pixel);
        }
		
		//[20070523][delete] send back the progress status
		//[20070523][delete] this.broadcastMessage("onProgress", mc, this.$pixeldata.rows, bmp.height)
		dispatchEvent({target:mc,type:"onProgress",result:{loaded:this.$pixeldata.rows,total:bmp.height}});
		
        this.$pixeldata.rows += 1
		
        if(this.$pixeldata.rows >= bmp.height){
            clearInterval(this.id)
			//[20070523][delete] completed!
			//[20070523][delete] this.broadcastMessage("onComplete", mc, this.$pixeldata);
			dispatchEvent({target:mc, type:"onComplete", result:{data:$pixeldata}});
            bmp.dispose();
        }
    }
	
}
